/*
 * File Name : su.cpp
 * Auther : dream.liuguobiao
 * Create Date : 2010-5-19
 * Last Edit : 2010-5-19
 */

//#include "user.h"
#include <iostream>

#include "su.h"
#include "my_string.h"

su::su( std::string *new_parameters)
:command(new_parameters)
{
}

int su::run_command(){
  shell_environment *my_environment = get_shell_environment();
  std::stack<user*> *login_users = my_environment->get_login_users();
  users *all_users = my_environment->get_all_users();
  user *tmp_user = my_environment->get_administrator() ;
  std::string input_string;
  my_string *string_tools = my_string::get_my_string();
  if( "" != *( get_parameters() ) && " " != *( get_parameters() ) ){
    if( *( get_parameters() ) != *(tmp_user->get_name() ) ){
      tmp_user = all_users->find_user_by_name( get_parameters() );
      if( NULL == tmp_user ){
        std::cout<<"Unknown id: "<<*( get_parameters())<<std::endl;
        return 0;
      }
    }
  }
if( 0 == login_users->top()->get_ID() ){
  login_users->push( tmp_user );
  std::cout<<std::endl;
  return 0;
 }
for(int i = 0 ; i < 3; i++){
  std::cout<<"Password: ";
  input_string = "";
  char c;
  while(1){
    c = string_tools->mygetch();
    if(c == '\n')break;
    input_string += c;
  }
  if(input_string == *(tmp_user->get_password()) ){
    login_users->push( tmp_user );
    std::cout<<std::endl;
    return 0;
  }
  std::cout<<std::endl<<"Sorry. You input wrong password for user : "<<*(tmp_user->get_name())<<std::endl;
}
// std::cout<<std::endl<<"Sorry. You input wrong password for user : "<<*(tmp_user->get_name())<<std::endl;
  return 0;
}
